package database;

public class BufferPool {
	int maxsizePool,sizePool,freePool,numberPages;
	Page[] pages;

	BufferPool(int sizePool){
		maxsizePool = sizePool;
		sizePool = 0;
		pages = new Page[1];
		freePool = maxsizePool;
		numberPages = 0;
	}

	public Page freeSpace(){		//free some place by returning a page that will be store in the hard drive and removing it from pages
		int i,min=0;
		for(i=0;i < this.numberPages; i++){
			if(pages[i].used < pages[min].used) min = i;
		}
		
		Page freepage = pages[min];
		i= min;
		for(i=min; i<this.numberPages; i++){
			pages[i]= pages[i+1];
		}
		pages[i]= null;
		this.numberPages--;
		
		this.sizePool -= freepage.size();
		this.freePool += freepage.size();
		
		return freepage;
	}
	
	public Page freeSpace(Page page){		 
		int i,min=0;
		
		if((pages[0].number == page.number) && ( (pages[0].tableName).compareTo(page.tableName) == 0  ) ) min = 1;
		
		for(i=0;i < this.numberPages; i++){
			if( (pages[i].used <= pages[min].used) && !( (pages[0].tableName).compareTo(page.tableName) == 0   ) ) min = i;
		}
		
		Page freepage = pages[min];
		i= min;
		for(i=min; i<this.numberPages-1; i++){
			pages[i]= pages[i+1];
		}
		pages[i]= null;
		this.numberPages--;
		this.sizePool -= freepage.size();
		this.freePool += freepage.size();

		return freepage;
	}
	
	
	public String addPage(Record record, Table table){
		
		Page[] pagesTemp = new Page[numberPages+1];
		int i;
		for(i=0 ; i < pages.length ; i++){
			pagesTemp[i] = pages[i];
		}
		pages = pagesTemp;
		
		
		Page newPage = new Page(table);
		String stringReturn;
		stringReturn = newPage.addRecord(record);
		pages[numberPages] = newPage;
		numberPages++;
		this.sizePool += newPage.size();
		this.freePool -= newPage.size();
		
		return stringReturn;
	}
	
	
	public void addPage(Page page){
		pages[numberPages] = page;
		numberPages++;
		this.sizePool += page.size();
		this.freePool -= page.size();
	}
	
	public boolean isLastPageOfTable(Table table){																			
		// will return true if the last page of table is in the buffer pool
		int i;
		boolean result = false;
		
		for(i=0; i< this.numberPages ; i++ ){
			if ((pages[i].number == table.numberPages) && (table.name.compareTo(pages[i].tableName) == 0)){
				result = true;
			}
		}
		
		return result;
	}
	
	public Page isPageOfTable(Table table, int page){		// will return the page number "page" of the table if it is in the buffer pool otherwise it returns null
		int i;
		Page result = null;
		
		for(i=0; i< this.numberPages ; i++ ){
			if ((pages[i].number == page) && (table.name.compareTo(pages[i].tableName) == 0)){
				result = pages[i];
			}
		}
		
		return result;
	}
	
	public Page getLastPageOfTable(Table table){																				
		//returns the last page of the table from the pool
		int i;
		Page result = new Page();																								/////// Pas tres propre pour l'initialisation de page voir avec une execption
		
		for(i=0; i< this.numberPages ; i++ ){
			if ((pages[i].number == table.numberPages) && (table.name.compareTo(pages[i].tableName) == 0)){
				result = pages[i];
			}
		}
		
		return result;
		
	}
}